package cn.iocoder.yudao.module.forward.service.broker;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.forward.controller.admin.broker.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.broker.BrokerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.forward.dal.mysql.broker.BrokerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.forward.enums.ErrorCodeConstants.*;

/**
 * 转发服务 Service 实现类
 *
 * @author zhang
 */
@Service
@Validated
public class BrokerServiceImpl implements BrokerService {

    @Resource
    private BrokerMapper brokerMapper;

    @Override
    public Integer createBroker(BrokerSaveReqVO createReqVO) {
        // 插入
        BrokerDO broker = BeanUtils.toBean(createReqVO, BrokerDO.class);
        brokerMapper.insert(broker);
        // 返回
        return broker.getId();
    }

    @Override
    public void updateBroker(BrokerSaveReqVO updateReqVO) {
        // 校验存在
        validateBrokerExists(updateReqVO.getId());
        // 更新
        BrokerDO updateObj = BeanUtils.toBean(updateReqVO, BrokerDO.class);
        brokerMapper.updateById(updateObj);
    }

    @Override
    public void deleteBroker(Integer id) {
        // 校验存在
        validateBrokerExists(id);
        // 删除
        brokerMapper.deleteById(id);
    }

    private void validateBrokerExists(Integer id) {
        if (brokerMapper.selectById(id) == null) {
            throw exception(BROKER_NOT_EXISTS);
        }
    }

    @Override
    public BrokerDO getBroker(Integer id) {
        return brokerMapper.selectById(id);
    }

    @Override
    public PageResult<BrokerDO> getBrokerPage(BrokerPageReqVO pageReqVO) {
        return brokerMapper.selectPage(pageReqVO);
    }

}